#include "Role.h"
#include "Logic/State/StateMachine.h"
#include "Message/MessagePump.h"
#include "Logic/State/StateWalk.h"
#include "Logic/State/StateNone.h"
#include "DXLib/DXInput.h"
#include "Render/Visual.h"
#include "Logic/Entity/EntityManager.h"
#include "Logic/Vector2D.h"
#include "Logic/State/StateAttack.h"
extern DXInput* g_pInput;

Role::Role(Visual* pVisual):LivingEntity(ENTITY_ROLE, pVisual)
{	m_cellPos.x() = 2;
m_cellPos.y() = 5;
	State* pDefaultState = new StateNone(this);
	AddState(pDefaultState);
	AddState(new StateWalk(this));
	AddState(new StateAttack(this));

	m_attribute.walkSpeed = 8;
	m_pStateMachine->SetDefaultState(pDefaultState);
}

void Role::Update()
{
	m_pStateMachine->UpdateMachine(1);
	if (g_pInput->IsMouseLDown())
	{
		POINT point = g_pInput->GetCurMousePoint();
		Vector2Di cell;
		cell.x() = point.x>>5;
		cell.y() = point.y>>5;
		std::map<int, BaseGameEntity*>::const_iterator itr = s_EntityManager->Begin();
		while (itr != s_EntityManager->End())
		{
			BaseGameEntity* pEntity = itr->second;
			if (pEntity->GetPos() == cell)
			{
				DataMessage<int>* newMsg = new DataMessage<int>(ENTITY_MSG|UNICAST_MSG, MSGTYPE_CHANGE_STATE,FSM_STATE_ATTACK);
				newMsg->m_toID = GetID();
				newMsg->m_flag = ENTITY_MSG | UNICAST_MSG;
				StateAttack* atkState = (StateAttack*)m_pStateMachine->GetState(FSM_STATE_ATTACK);
				atkState->SetGoal(pEntity);
				s_MessagePump->SendMsg(newMsg);
				return;
			}
			itr++;
		}
		State* pCurState = m_pStateMachine->GetCurrentState();
		StateWalk* pNextState = (StateWalk *)m_pStateMachine->GetState(FSM_STATE_WALK);
		pNextState->SetDestCell(Vector2Di(point.x>>5,point.y>>5));
		if (pCurState->m_type != FSM_STATE_WALK)
		{
			DataMessage<int>* newMsg = new DataMessage<int>(ENTITY_MSG|UNICAST_MSG, MSGTYPE_CHANGE_STATE,FSM_STATE_WALK);
			newMsg->m_toID = GetID();
			newMsg->m_flag = ENTITY_MSG | UNICAST_MSG;
			s_MessagePump->SendMsg(newMsg);
		}

	}

}
